package cn.iocoder.yudao.module.hs.service.sap;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.material.vo.MaterialSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialbox.vo.MaterialBoxSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rawmaterialorder.vo.RawMaterialOrderSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.sap.vo.MaterialSyncVO;
import cn.iocoder.yudao.module.hs.controller.admin.sap.vo.RawMaterialOrderSyncVO;
import cn.iocoder.yudao.module.hs.controller.admin.sap.vo.ShipOrderSyncVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporder.vo.ShipOrderSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.tray.vo.TraySaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.material.MaterialDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.materialbox.MaterialBoxDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.tray.TrayDO;
import cn.iocoder.yudao.module.hs.service.material.MaterialService;
import cn.iocoder.yudao.module.hs.service.materialbox.MaterialBoxService;
import cn.iocoder.yudao.module.hs.service.rawmaterialorder.RawMaterialOrderService;
import cn.iocoder.yudao.module.hs.service.shiporder.ShipOrderService;
import cn.iocoder.yudao.module.hs.service.tray.TrayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.*;

/**
 * SAP数据同步 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class SapDataSyncServiceImpl implements SapDataSyncService {

    @Resource
    private TrayService trayService;

    @Resource
    private MaterialBoxService materialBoxService;

    @Resource
    private MaterialService materialService;

    @Resource
    private ShipOrderService shipOrderService;

    @Resource
    private RawMaterialOrderService rawMaterialOrderService;

    @Override
    @Transactional
    public void syncMaterial(List<MaterialSyncVO> materialSyncVOS) {
        if (CollUtil.isEmpty(materialSyncVOS)) {
            throw exception(SAP_DATA_SYNC_MATERIAL_EMPTY);
        }
        for (MaterialSyncVO materialSyncVO : materialSyncVOS) {
//            if (StrUtil.isEmpty(materialSyncVO.getTrayCode())) {
//                throw exception(SAP_DATA_SYNC_MATERIAL_TRAY_CODE_EMPTY);
//            }
//            if (StrUtil.isEmpty(materialSyncVO.getMaterialCode())) {
//                throw exception(SAP_DATA_SYNC_MATERIAL_CODE_EMPTY);
//            }
//            if (StrUtil.isEmpty(materialSyncVO.getMaterialModeCode())) {
//                throw exception(SAP_DATA_SYNC_MATERIAL_MODECODE_EMPTY);
//            }
            TrayDO tray = trayService.getTray(materialSyncVO.getTrayCode());
            if (tray == null) {
                TraySaveReqVO traySaveReqVO = new TraySaveReqVO();
                traySaveReqVO.setCode(materialSyncVO.getTrayCode());
                traySaveReqVO.setWarehouseId(materialSyncVO.getWarehouseId());
                Long trayId = trayService.createTray(traySaveReqVO);
                tray = trayService.getTray(trayId);
            }
            MaterialBoxDO materialBox = null;
            if (StrUtil.isNotEmpty(materialSyncVO.getBoxCode())) {
                materialBox = materialBoxService.getMaterialBox(materialSyncVO.getBoxCode());
                if (materialBox == null) {
                    MaterialBoxSaveReqVO boxSaveReqVO = new MaterialBoxSaveReqVO();
                    boxSaveReqVO.setCode(materialSyncVO.getBoxCode());
                    boxSaveReqVO.setWarehouseId(materialSyncVO.getWarehouseId());
                    boxSaveReqVO.setTrayCode(tray.getCode());
                    Long boxId = materialBoxService.createMaterialBox(boxSaveReqVO);
                    materialBox = materialBoxService.getMaterialBox(boxId);
                }
            }
            if (StrUtil.isNotEmpty(materialSyncVO.getBoxCode2())) {
                materialBox = materialBoxService.getMaterialBox(materialSyncVO.getBoxCode2());
                if (materialBox == null) {
                    MaterialBoxSaveReqVO boxSaveReqVO = new MaterialBoxSaveReqVO();
                    boxSaveReqVO.setCode(materialSyncVO.getBoxCode2());
                    boxSaveReqVO.setWarehouseId(materialSyncVO.getWarehouseId());
                    boxSaveReqVO.setTrayCode(tray.getCode());
                    Long boxId = materialBoxService.createMaterialBox(boxSaveReqVO);
                    materialBox = materialBoxService.getMaterialBox(boxId);
                }
            }
            MaterialDO material = materialService.getMaterial(materialSyncVO.getMaterialCode());
            if (material != null) {
                MaterialSaveReqVO updateReqVO = BeanUtils.toBean(material, MaterialSaveReqVO.class);
                if (materialBox != null) {
                    updateReqVO.setBoxCode(materialBox.getCode());
                }
                updateReqVO.setTrayCode(tray.getCode());
                materialService.updateMaterial(updateReqVO);
            } else {
                MaterialSaveReqVO materialSaveReqVO = new MaterialSaveReqVO();
                if (materialBox != null) {
                    materialSaveReqVO.setBoxCode(materialBox.getCode());
                }
                materialSaveReqVO.setTrayCode(tray.getCode());
                materialSaveReqVO.setWarehouseId(materialSyncVO.getWarehouseId());
                materialSaveReqVO.setModeCode(materialSyncVO.getMaterialModeCode());
                materialSaveReqVO.setWorkOrderNo(materialSyncVO.getWorkOrderNo());
                materialSaveReqVO.setBatchNo(materialSyncVO.getBatchNo());
                materialSaveReqVO.setCode(materialSyncVO.getMaterialCode());
                materialSaveReqVO.setCode2(materialSyncVO.getMaterialCode2());
                materialService.createMaterial(materialSaveReqVO);
            }
        }
    }

    @Override
    public void syncShipOrder(ShipOrderSyncVO shipOrderSyncVO) {
        if (StrUtil.isEmpty(shipOrderSyncVO.getCode())) {
            throw exception(SAP_DATA_SYNC_SHIP_ORDER_CODE_EMPTY);
        }
        if (CollUtil.isEmpty(shipOrderSyncVO.getShipOrderDetails())) {
            throw exception(SAP_DATA_SYNC_SHIP_ORDER_DETAIL_EMPTY);
        }
        ShipOrderSaveReqVO shipOrderSaveReqVO = BeanUtils.toBean(shipOrderSyncVO, ShipOrderSaveReqVO.class);
        shipOrderSaveReqVO.setShipOrderDetailList(shipOrderSyncVO.getShipOrderDetails());
        shipOrderService.createShipOrder(shipOrderSaveReqVO);
    }

    @Override
    public void syncRawMaterialOrder(List<RawMaterialOrderSyncVO> rawMaterialOrderSyncVOS) {
        List<RawMaterialOrderSaveReqVO> saveReqVOS = BeanUtils.toBean(rawMaterialOrderSyncVOS, RawMaterialOrderSaveReqVO.class);
        for (RawMaterialOrderSaveReqVO saveReqVO : saveReqVOS) {
            rawMaterialOrderService.createRawMaterialOrder(saveReqVO);
        }
    }
}
